<?php
session_start();
//require_once("../config/include.inc.php");
require_once("../config/include_tokyo.inc.php");
require_once("../class/Core_Lib_Page.php");
require_once("../class/Common_Function.php");
$commonFunction = new Common_Function();

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 || $userRole == 4){
	//管理员,订单管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//get post value
$doFlag = $_POST["doFlag"];
$id = $_POST["id"];
$validateFlag = true;
$insertError = "";

if($doFlag == "addTransfer"){
	//新增记录
	$bank_time_str =  $_POST["bank_time"];
	$bank_time = strtotime($bank_time_str." 03:00:00");
	if($bank_time == "" || $bank_time == false || $bank_time == -1){
		$validateFlag = false;
		$insertError = $insertError."  转帐时间格式不对";
	}else{
		$bank_time = date("Y-m-d",$bank_time); 
	}
	$transfer_flag =  $_POST["transfer_flag"];
	$bank_amount =  $_POST["bank_amount"];
	$bank_amount = str_replace(",","",$bank_amount);
	$bank_amount = trim($bank_amount);
	if($transfer_flag != 3){//归总时，可能有负数
		if(eregi("^[0-9]+$",$bank_amount)){
			//是数字
			if($transfer_flag == "1" && $bank_amount > 0){
				//资金转出
				$bank_amount = $bank_amount*-1;
			}
		}else{
			$validateFlag = false;
			$insertError = $insertError."  金额只能是数字";
		}
	}	
    $bank_note =  $_POST["bank_note"];
	$owner_flag =  $_POST["owner_flag"];
	$order_no =  $_POST["order_no"];
	if($owner_flag == 0 && $order_no == "" && $transfer_flag == "0"){
		$validateFlag = false;
		$insertError = $insertError."  订单编号不能为空";
	}
	$customer_name =  $_POST["customer_name"];
	$our_note =  $_POST["our_note"];	
	$stauts_flag =  0;
	if($validateFlag){
		$sql = "insert into banktransfer(bank_time,bank_amount,bank_note,owner_flag,order_no,customer_name,our_note,transfer_flag,stauts_flag) values ('$bank_time',$bank_amount,'$bank_note',$owner_flag,'$order_no','$customer_name','$our_note',$transfer_flag,$stauts_flag)";
		$db->Execute($sql);	
	}		
}else if($doFlag == "doDelete"){
	//删除记录
	$id =  $_POST["id"];
	$sql = "delete from banktransfer where id=".$id;
	$db->Execute($sql);	
}else if($doFlag == "doClear"){
	//全部转为历史数据
	$sql = "SELECT SUM(bank_amount) as bank_sum FROM banktransfer where owner_flag=0 and stauts_flag=0";
	$result = $db->Execute($sql);	
	if (empty($result) ||  $result->RecordCount() < 1) {	
		$vincent_sum = 0;
	}else{
		$vincent_sum = $result->fields['bank_sum'];
	}
	$sql = "SELECT SUM(bank_amount) as bank_sum   FROM banktransfer where owner_flag=1 and stauts_flag=0";
	$result = $db->Execute($sql);	
	if (!empty($result)) {
		$liuyi_sum = $result->fields['bank_sum'];
	}else{
		$liuyi_sum = 0;
	}

	$sql = "update banktransfer set stauts_flag = 1 where stauts_flag = 0";
	$db->Execute($sql);	

	$bank_time = date("Y-m-d"); 
	$bank_amount = $vincent_sum;
	$bank_note = "";
	$owner_flag = 0;
	$order_no = "";
	$customer_name = "";
	$our_note = "小结 重新归总";
	$transfer_flag = 3;
	$stauts_flag = 0;
	$sql = "insert into banktransfer(bank_time,bank_amount,bank_note,owner_flag,order_no,customer_name,our_note,transfer_flag,stauts_flag) values ('$bank_time',$bank_amount,'$bank_note',$owner_flag,'$order_no','$customer_name','$our_note',$transfer_flag,$stauts_flag)";
	$db->Execute($sql);	

	$bank_amount = $liuyi_sum;
	$owner_flag = 1;
	$sql = "insert into banktransfer(bank_time,bank_amount,bank_note,owner_flag,order_no,customer_name,our_note,transfer_flag,stauts_flag) values ('$bank_time',$bank_amount,'$bank_note',$owner_flag,'$order_no','$customer_name','$our_note',$transfer_flag,$stauts_flag)";
	$db->Execute($sql);

}


//统计总数
$sql = "SELECT SUM(bank_amount) as bank_sum FROM banktransfer where owner_flag=0 and stauts_flag=0";
$result = $db->Execute($sql);	
if (empty($result) ||  $result->RecordCount() < 1) {	
	$vincent_sum = 0;
}else{
	$vincent_sum = $result->fields['bank_sum'];
}
$sql = "SELECT SUM(bank_amount) as bank_sum   FROM banktransfer where owner_flag=1 and stauts_flag=0";
$result = $db->Execute($sql);	
if (!empty($result)) {
	$liuyi_sum = $result->fields['bank_sum'];
}else{
	$liuyi_sum = 0;
}
$bank_self = 1612;//2012-12-17 百五上的利息
$bank_sum = $vincent_sum + $liuyi_sum +  $bank_self;



//分页设置
$list_rows = 30; //每页显示的记录数
if($doFlag == "showAll"){
	//全部显示
	$sql_list = "SELECT COUNT(*) as total_rows  from banktransfer"; 
}else if($doFlag == "doSearchDate"){
	//搜索: 按日期
	$bank_time_search =  $_POST["bank_time_search"];	
	$sql_list = "SELECT COUNT(*) as total_rows  from banktransfer where bank_time ='".$bank_time_search."' "; 
}else if($doFlag == "doSearchNote"){
	//搜索: 按摘要
	$bank_note_search =  $_POST["bank_note_search"];	
	$sql_list = "SELECT COUNT(*) as total_rows  from banktransfer where bank_note  like'%".$bank_note_search."%' "; 
}else if($doFlag == "doSearchTransfer"){
	//搜索: 按类型
	$transfer_flag_search =  $_POST["transfer_flag_search"];	
	$sql_list = "SELECT COUNT(*) as total_rows  from banktransfer where transfer_flag ='".$transfer_flag_search."' "; 
}else{
	//默认: 当前数据
	$sql_list = "SELECT COUNT(*) as total_rows  from banktransfer  where stauts_flag=0"; 
}
$result = $db->Execute($sql_list);
if (!empty($result)) {
	$totalRows = $result->fields['total_rows'];
}
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => !empty($_POST['nowPage']) ? $_POST['nowPage'] : 1 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;


//get bank list,默认只列出 stauts_flag=0的,0=正常显示 1=历史数据
if($doFlag == "showAll"){
	//全部显示
	$sql_select = "select * from banktransfer order by bank_time desc,modifytime desc limit $limit_m,$limit_n";
}else if($doFlag == "doSearchDate"){
	//搜索: 按日期
	$bank_time_search =  $_POST["bank_time_search"];	
	$sql_select = "select * from banktransfer where bank_time ='".$bank_time_search."'  order by bank_time desc,modifytime desc limit $limit_m,$limit_n";
}else if($doFlag == "doSearchNote"){
	//搜索: 按摘要
	$bank_note_search =  $_POST["bank_note_search"];	
	$sql_select = "select * from banktransfer where bank_note  like'%".$bank_note_search."%' order by bank_time desc,modifytime desc limit $limit_m,$limit_n";
}else if($doFlag == "doSearchTransfer"){
	//搜索: 按类型
	$transfer_flag_search =  $_POST["transfer_flag_search"];	
	$sql_select = "select * from banktransfer where transfer_flag='".$transfer_flag_search."' order by bank_time desc,modifytime desc limit $limit_m,$limit_n";
}else{
	//默认: 当前数据
	$sql_select = "select * from banktransfer where  stauts_flag=0 order by bank_time desc,modifytime desc limit $limit_m,$limit_n";
}
$result = $db->Execute($sql_select);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "银行转帐列表"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>




<form name="listForm" action="bankList.php" method="post">
<div id="container" >	




<div class="grayTable">	
<h4>新增银行记录:
<a href="https://www.105direct.105bank.com/WBIG0000.html" target="_blank">百五</a> 1763308176
</h4>
<p><font color='red'><b>从2013.5.1开始,vincent停止百五收款。(很少老客户可能还会直接银行付款)</b></font></p>

<?php
$currentDate = date('Y-m-d',time());
if(!$validateFlag){
	echo "<font color='red'>新增记录出错: ".$insertError."</font>";
}
?>
<!----------------新增记录----------------------------->
<table >
<thead>
	<tr>
		<th scope="col" >转帐时间</th>
	    <th scope="col" >转帐金额</th>
		<th scope="col" >银行摘要</th>
		<th scope="col" >记录类型</th>
		<th scope="col" >资金流向</th>
		<th scope="col" >订单编号</th>
		<th scope="col" >客户名字</th>
		<th scope="col" >备注</th>
		<th scope="col" >操作</th>
	</tr>
	<tr>
		<td><input type="text" name="bank_time" id="bank_time" value="<?php echo $currentDate;?>" size=8></td>
		<td><input type="text" name="bank_amount" id="bank_amount"  size=8></td>
		<td><input type="text" name="bank_note" id="bank_note"  size=15></td>
		<td>
			<select name="transfer_flag" id="transfer_flag">
			<option value=0  >转入</option>
			<option value=1  >转出</option>
			<option value=2  >疑问</option>
			<option value=3  >归总</option>
			</select>
		</td>
		<td>
			<select name="owner_flag" id="owner_flag">
			<option value=0  >Vincent</option>
			<option value=1  >LiuYi</option>
			</select>
		</td>
		<td><input type="text" name="order_no" id="order_no"  size=8></td>
		<td><input type="text" name="customer_name" id="customer_name"  size=15></td>
		<td><input type="text" name="our_note" id="our_note"  size=15></td>
		<td><input type="button" name="提交" value="提交" onclick="addTransfer()"></td>
	</tr>
</thead>					
</table>
<br>


<!----------------查询记录----------------------------->
<table >
<thead>
<tr>
<td>时间</td><td><input type="text" name="bank_time_search" id="bank_time_search" value="<?php echo $currentDate;?>" size=8></td><td><input type="button" name="搜索" value="搜索" onclick="doSearchDate()"></td>
<td>摘要</td><td><input type="text" name="bank_note_search" id="bank_note_search"  size=8></td><td><input type="button" name="搜索" value="搜索" onclick="doSearchNote()"></td>
<td>类型</td><td><select name="transfer_flag_search" id="transfer_flag_search"><option value=2  >疑问</option><option value=0  >转入</option><option value=1  >转出</option><option value=3  >归总</option></select></td><td><input type="button" name="搜索" value="搜索" onclick="doSearchTransfer()"></td>
</tr>
</thead>
</table>

<h4>
<a href='/bank/bankList.php' >当前统计:</a>
(Vincent[<?php echo $commonFunction->toMoney($vincent_sum);?>]
<!--
+
LiuYi[<?php echo $commonFunction->toMoney($liuyi_sum);?>]
+
BankSelf[<?php echo $commonFunction->toMoney($bank_self);?>]
 =
百五[<?php echo $commonFunction->toMoney($bank_sum);?>] )
-->
</h4>


<!----------------记录list----------------------------->
<table >
<thead>
	<tr>
		<th scope="col" width=30>编号</th>
		<th scope="col" width=80>时间</th>
	    <th scope="col" width=60>金额</th>
		<th scope="col" >摘要</th>
		<th scope="col" width=60>归属</th>
		<th scope="col" width=60>订单</th>
		<th scope="col" >客户</th>
		<th scope="col" >备注</th>
		<th scope="col" >类型</th>
		<th scope="col" >操作</th>
	</tr>
</thead>

<tbody>	
	<?php	
	if(empty($result) || $result->RecordCount() < 1){
		echo "<tr><td colspan=10 align='center'>记录为空.</td></tr>";
	}else{
		while (!$result->EOF) {	
			$owner = $result->fields['owner_flag'];
			if($owner == 0){
				$owner = "Vincent";
			}else if($owner == 1){
				$owner = "LiuYi";
			}else{
				$owner = "未知";
			}
			$transfer_name = $result->fields['transfer_flag'];
			$importantClass = "class='important'";
			if($transfer_name == 0){
				$transfer_name = "转入";
				$importantClass = "";
			}else if($transfer_name == 1){
				$transfer_name = "转出";
			}else if($transfer_name == 2){
				$transfer_name = "疑问";
			}else if($transfer_name == 3){
				$transfer_name = "归总";
			}else{
				$transfer_name = "未知";
			}
			$stauts_flag_str = '';
			$stauts_flag = $result->fields['stauts_flag'];
			if($stauts_flag  == 1){
				$stauts_flag_str = "<br>(历史记录)";
			}else{
				$stauts_flag_str = '';
			}
			$strTR = "<tr  ".$importantClass." >";
			$strTR = $strTR."<td>".$result->fields['id']."</td>";
			$strTR = $strTR."<td>".date('Y-m-d',strtotime($result->fields['bank_time']))."</td>";
			$strTR = $strTR."<td>".$commonFunction->toMoney($result->fields['bank_amount'])."</td>";
			$strTR = $strTR."<td>".$result->fields['bank_note']."</td>";
			$strTR = $strTR."<td>".$owner."</td>";
			$strTR = $strTR."<td>".$result->fields['order_no']."</td>";
			$strTR = $strTR."<td>".$result->fields['customer_name']."</td>";
			$strTR = $strTR."<td>".$result->fields['our_note']."</td>";
			$strTR = $strTR."<td>".$transfer_name.$stauts_flag_str."</td>";
			$strTR = $strTR."<td>";
			$strTR = $strTR."<input type='button' value='编辑' onclick='doEdit(".$result->fields['id'].")'>";
			$strTR = $strTR."<input type='button' value='删除' onclick='doDelete(".$result->fields['id'].")'>";
			$strTR = $strTR."</td>";
			$strTR = $strTR."</tr>";
			echo $strTR;
			$result->MoveNext();
		}
	}
	?>
</tbody>	


					
</table>	
<br>
<!--Page 分页 -->
<div id="page"><?php echo $page->show(2);?></div>

<div class="clearBoth">&nbsp;</div>

<?php if($userRole == 1){?>
<div>
<a href="javascript:doClear()">清理数据</a>
<a href="javascript:showAll()">查看全部</a>
</div>
<?php }?>

</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" value="<?php echo $doFlag; ?>">
<input type="hidden" name="id" id="id" value="<?php echo $id; ?>">
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>">
</form>

</body>




</html>

<script>
/************新增记录**************************/
function addTransfer(){
	var bank_amount = $("#bank_amount").val();	
	if(bank_amount==""){
		alert("转帐金额不能为空");
		$("#bank_amount").focus();  
		return;
	}
	var bank_note = $("#bank_note").val();	
	if(bank_note==""){
		alert("银行摘要不能为空");
		$("#bank_note").focus();  
		return;
	}
	document.getElementById('doFlag').value = "addTransfer";
	document.listForm.submit();
}

/************翻页跳转**************************/
function goToPage(nowPage){	
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}


/************删除记录**************************/
function doDelete(id){	
	if(confirm("确认要删除编号为: "+id+" 的记录?")){
		document.getElementById('doFlag').value = "doDelete";
		document.getElementById('id').value = id;
		document.listForm.submit();
	}	
}

/************清理数据**************************/
function doClear(id){	
	if(confirm("清理数据将导致现有记录不再显示,不再被统计在内,是否继续?")){
		if(confirm("请记录当前统计数据,Vincent[<?php echo $commonFunction->toMoney($vincent_sum);?>],LiuYi[<?php echo $commonFunction->toMoney($liuyi_sum);?>]")){
			document.getElementById('doFlag').value = "doClear";
			document.listForm.submit();	
		}			
	}
}


/************查看全部记录**************************/
function showAll(){	
	document.getElementById('doFlag').value = "showAll";
	document.listForm.submit();	
}


/************编辑记录**************************/
function doEdit(id){	
	window.location.href="bankInput.php?id="+id; 
}

/************搜索: 按日期**************************/
function doSearchDate(){
	document.getElementById('doFlag').value = "doSearchDate";
	document.listForm.submit();
}

/************搜索: 按摘要**************************/
function doSearchNote(){
	document.getElementById('doFlag').value = "doSearchNote";
	document.listForm.submit();
}

/************搜索: 按类型**************************/
function doSearchTransfer(){
	document.getElementById('doFlag').value = "doSearchTransfer";
	document.listForm.submit();
}


</script>

